=begin rdoc
Definition File for 1129-reduced-initset.evl eventset

Author::    Matthias Woehrle  (mailto:matthias.woehrle@tik.ee.ethz.ch)
Copyright:: Copyright (c) 2008 ETH Zurich
License::   

TODO:

MISSING:

=end
$: << File.expand_path(File.dirname(__FILE__) + "/../lib")
sourcepath =  File.expand_path("~/Documents/ETH/tecdoc/woehrlem/paper/woehrlem2008_sensys_poster/trunk/src/tossim/")
require "rupeas"

Rupeas.new do

Type :senddone do
 with :name =>:seqNo, :fieldtype=> :periodic, :range => 0..255, :notification => :error
 with :name =>:origin, :fieldtype => :integer, :range => 0..100,:notification => :error
 with :name =>:destination, :fieldtype => :integer, :range => 0..100, :notification => :warning
 with :name =>:nodeid,:fieldtype=> :integer, :range => 0..100,:notification => :error
 with :name =>:time, :fieldtype => :float, :notification => :warning 
end

Type :receive do
 with :name =>:seqNo, :fieldtype=> :periodic, :range => 0..255, :notification => :error
 with :name =>:origin, :fieldtype => :integer, :range => 0..100,:notification => :error
 with :name =>:destination, :fieldtype => :integer, :range => 0..100, :notification => :warning
 with :name =>:nodeid,:fieldtype=> :integer, :range => 0..100,:notification => :error
 with :name =>:time, :fieldtype => :float, :notification => :warning 
end

all = loadlog(sourcepath + '/small.log')
total_packets = 0
total_duplicates = 0
total_sent = 0
#       r = File.open('logfile.txt', 'w')
#       [10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,0,43,44,45,46,100,48,49,50,51,52,53,
#         54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79].each do |origin|
#       #[13, 14, 15].each do |origin|
#         origin_sent = 0
#         origin_packets = 0
#         duplicates = 0
#       
#         if all[:origin=>origin].length > 0
#           r.write "------------\nProcessing #{all[:origin=>origin].length} events for origin #{origin}\n------------\n"
#           originfiltered = all[:origin=>origin]
#            #p all.values(:seqNo)
#             puts "------------\nProcessing #{originfiltered.length} events for origin #{origin}\n"
#             all.values(:seqNo).each do |seqNo|
#       #      puts "------------\nProcessing #{originfiltered[:seqNo=>seqNo].length} events for origin #{origin} and sequence Number #{seqNo}\n"
#              
#              routestart_temp = originfiltered[:seqNo=>seqNo].predicate(1){|send| send[:origin] == send[:nodeid] and send[:type] == :senddone}
#              routestart = transform(routestart_temp, :merge, 1) do |e1|
#                   {
#                     :type => :transmission,
#                     :nodeid => e1[:nodeid],
#                     :destination => e1[:destination],
#                     :seqNo => e1[:seqNo],
#                     :origin => e1[:origin],
#                     :time => e1[:time]
#                   }
#                   end
#                   
#              packet_starts = routestart[:type=>:transmission].length
#              
#       #       r.write originfiltered[:seqNo=>seqNo].to_s
#                     
#              resultset = routestart.iterative do |iteration|
#                 temp = iteration.predicate(2) {|e1, e2| e1[:origin] == e2[:origin] and  e1[:origin] == e2[:origin] and ((e1[:type] == :transmission and \
#                 e2[:type] == :receive and e1[:destination] == e2[:nodeid]) or (e1[:nodeid] == e2[:nodeid] and
#                 e1[:type] == :transmission and e2[:type] == :senddone))
#                 }
#                 iteration.transform(temp, :merge, 2) do |e1, e2|
#                 {
#                   :nodeid => e2[:nodeid],
#                   :type => :transmission,
#                   :origin => e1[:origin],
#                   :destination => e2[:destination],
#                   :seqNo => e1[:seqNo],
#                   :time => e2[:time]            
#                 }
#                end
#              end
#              
#              transmission = resultset[:type=>:transmission]
#       
#              transmissions_to_0 = transmission[:nodeid=>0]
#              transmissions_to_100 = transmission[:nodeid=>100]
#              success =     transmissions_to_0.length+transmissions_to_100.length
#              
#              if packet_starts > 0
#       #         r.write "origin: #{origin}, sequence number: #{seqNo} => #{success}\n" unless success == 1
#                duplicates = duplicates + 1 if success > 1 
#                total_duplicates = total_duplicates + 1 if success > 1 
#                total_packets = total_packets + 1 if success > 0
#                total_sent = total_sent + 1 if packet_starts > 0
#                origin_sent = origin_sent + 1 if packet_starts > 0
#                origin_packets = origin_packets + 1 if success > 0
#              else 
#                r.write "\nGhost packet for #{origin}/#{seqNo}\n" if success > 0
#              end
#           end   
#             r.write "Yield for origin #{origin} is #{origin_packets.to_f/origin_sent}\n"
#             r.write "Duplicate rate for origin #{origin} is #{duplicates.to_f/origin_sent}\n"      
#             r.flush
#         end
#       
#       end
#         r.write "Total Yield is #{total_packets.to_f/total_sent}\n"
#         r.write "Duplicate rate is #{total_duplicates.to_f/total_sent}\n"        
#         
#       r.close 
end
